package com.fjwt.gz.mch.ctrl.proxy;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.vo.AwardCouponVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.FansAwardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/proxy/fansAward")
public class FansAwardProxyController extends CommonCtrl {

    @Autowired
    private FansAwardService fansAwardService;

    /**
     * 卡券发放列表
     */
    @PostMapping("/sendCouponList")
    public ApiRes sendCouponList() {
        Long activityId = getValLong("activityId");
        String phone = getValString("phone");
        String nickName = getValString("nickName");
        String realName = getValString("realName");
        String createdAt = getValString("createdAt");
        String locationAgentNo =request.getHeader("locationAgentNo");
        String appId = request.getHeader("appid");
        log.info("卡券发放接口被调用：请求地址：/api/fansAward/sendCouponList 活动id：{}，用户手机号：{}，用户名称：{}，发放时间：{}", activityId, phone, nickName, createdAt);
        if (Objects.isNull(activityId)) {
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        IPage<AwardCouponVO> list = fansAwardService.sendCouponList(getIPage(), activityId, phone, nickName, realName, createdAt, getCurrentUser().getFansId(),locationAgentNo,appId);
        return ApiRes.page(list);
    }

    /**
     * 用户领取卡券详情
     */
    @PostMapping("/fansCouponDetail")
    public ApiRes fansCouponDetail() {
        Long fansAwardId = getValLong("fansAwardId");
        log.info("用户领券详情接口调用：请求地址：/api/fansAward/fansCouponDetail 用户奖品fansAwardId：{}", fansAwardId);
        if (Objects.isNull(fansAwardId)) {
            return ApiRes.customFail("网络故障，请稍后重试！");
        }
        String locationAgentNo =request.getHeader("locationAgentNo");
        AwardCouponVO couponDetail = fansAwardService.fansCouponDetail(fansAwardId,locationAgentNo);
        AwardCouponVO data = new AwardCouponVO();
        BeanUtils.copyProperties(couponDetail, data);
        return ApiRes.ok(data);
    }
}
